package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.user.dto.UserRegisterDTO;
import com.zhuanzhuan.hero.user.enums.ClientSourceEnum;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.helper.ZzLockHelper;
import com.zhuanzhuan.hero.user.helper.ZzUserHelper;
import com.zhuanzhuan.hero.user.service.IUserService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.zzlock.lock.ZZEtcdLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName UserRegisterFacade
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/17 17:51
 */
@ServiceBehavior
@Slf4j
public class UserRegisterFacadeImpl implements IUserRegisterFacade {

    @Resource
    private IUserService userService;

    @Resource
    private ZzLockHelper lockHelper;

    @Resource
    private ZzUserHelper zzUserHelper;

    @ZZMethod(value = "register", desc = "注册")
    @Override
    public ZZOpenScfBaseResult<Boolean> register(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") UserRegisterDTO userRegisterDTO) {
        log.info("k=s act=register param={}", JsonUtils.toJsonWithoutNull(userRegisterDTO));

        Long uid;
        String mobilePhone;
        String tValue = ServiceHeaderUtil.parseTvalue(header);
        if (ClientSourceEnum.WECHAT_MINI.getTValue().equals(tValue)) {
            uid = ServiceHeaderUtil.parseUid(header);
            //如果是小程序，入参没有mobile，取转转用户的手机号
            log.info("小程序注册，使用转转用户手机号");
            mobilePhone = zzUserHelper.getRealMobileByUid(uid);
        } else {
            uid = userRegisterDTO.getUid();
            mobilePhone = userRegisterDTO.getMobilePhone();
        }

        if (Objects.isNull(uid) || StringUtils.isBlank(mobilePhone)) {
            //有可能是中台查询不到mobilePhone  返回401让小程序重新登录
            throw new BusinessException(ExceptionMsgEnum.LOGIN_ERROR);
        }

        try (ZZEtcdLock lock = lockHelper.getLock("register_" + uid)) {
            if (!lock.tryLock()) {
                log.info("获取锁失败");
                return ZZOpenScfBaseResult.buildErr(ExceptionMsgEnum.COMMON_ERROR.getCode(), ExceptionMsgEnum.COMMON_ERROR.getErrorMsg());
            }
            if (!userService.doRegister(uid, mobilePhone)) {
                return ZZOpenScfBaseResult.buildErr(ExceptionMsgEnum.REGISTER_FAIL.getCode(), ExceptionMsgEnum.REGISTER_FAIL.getErrorMsg());
            }
            return ZZOpenScfBaseResult.buildSucc(true);
        } catch (Exception e) {
            log.error("doRegister错误", e);
            if (ClientSourceEnum.isOuterClient(tValue)) {
                //小程序返回特殊文案
                return ZZOpenScfBaseResult.buildErr(ExceptionMsgEnum.REGISTER_FAIL.getCode(), "注册失败，请重新进入小程序");
            }
            throw e;
        }
    }
}
